# Your RandomizedCollection object will be instantiated and called as such:
# obj = RandomizedCollection()
# param_1 = obj.insert(val)
# param_2 = obj.remove(val)
# param_3 = obj.getRandom()
from collections import defaultdict
import random


class RandomizedCollection:
    def __init__(self):
        self.m_dict = defaultdict(list)
        self.m_list = list()

    def insert(self, val: int) -> bool:
        self.m_dict[val].append(len(self.m_list))
        self.m_list.append(val)
        return len(self.m_dict[val]) == 1

    def remove(self, val: int) -> bool:
        if val in self.m_dict:
            index = self.m_dict[val].pop(-1)
            if not self.m_dict[val]:
                self.m_dict.pop(val)
            try:
                self.m_list[index] = self.m_list.pop(-1)
                self.m_dict[self.m_list[index]].remove(len(self.m_list))
                self.m_dict[self.m_list[index]].append(index)
            except:
                pass
            return True
        return False

    def getRandom(self) -> int:
        return self.m_list[random.randint(0, len(self.m_list)-1)]

